package Java.BlackHorse.a06ThreadTest.Test4;

import Java.BlackHorse.a06ThreadTest.Test3.MyThread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    ArrayList<Integer> list = new ArrayList<>();

    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {

        ArrayList<Integer> boxlist = new ArrayList<>();
        while (true) {
            synchronized (MyThread.class) {
                if (list.size() == 0) {
                    if ("抽奖箱一".equals(Thread.currentThread().getName())) {
                        System.out.println("抽奖箱一" + boxlist);
                        System.out.println("总金额为：" + getSum(boxlist));
                    } else {
                        System.out.println("抽奖箱二" + boxlist);
                        System.out.println("总金额为：" + getSum(boxlist));
                    }
                    break;
                }
                Collections.shuffle(list);

                if ("抽奖箱一".equals(Thread.currentThread().getName())) {
                    boxlist.add(list.remove(0));
                } else {
                    boxlist.add(list.remove(0));
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return Collections.max(boxlist);
    }
    public int getSum(ArrayList<Integer> list){
        int sum=0;
        for(Integer i : list){
            sum+=i;
        }
        return sum;
    }
}
